#include<iostream>
#include<func.h>
using std::cout;
using std::endl;
class String
{
public:
    String();
    String(const char*pstr);
    String(const String &rhs);
    String &operator=(const String &rhs);
    ~String();
    void print();
    size_t length() const;
    const char*c_str() const;
private:
    char *_pstr;

};
//默认构造
String::String()
:_pstr(new char('0'))
{
    
}
//拷贝构造
String::String(const char*pstr)
:_pstr(new char[strlen(pstr)+1]())
{
    strcpy(_pstr,pstr);
}
//计算str的长度
size_t String::length() const{
    
    return strlen(this->_pstr)+1;
}
//字符串转换 写在函数名前面
const char* String::c_str() const{
    return _pstr;
}
void String::print(){
    cout<<_pstr<<endl;
}
//拷贝构造
String::String(const String &rhs)
:_pstr(new char[rhs.length()]())
{
    strcpy(_pstr,rhs.c_str());
    cout<<"string(const str &rhs)"<<endl;
}
//赋值运算符
String &String::operator=(const String &rhs){
    //考虑自复制
    if(this != &rhs){
        delete [] _pstr;
        _pstr = new char[rhs.length()]();
        strcpy(_pstr,rhs.c_str());
    }
    cout<<"string &operator = (const string&)"<<endl;
    return *this; //返回的是值
}

String::~String(){
    if(!_pstr){
        delete [] _pstr;
    }
}

int main(void){
    String str;
    str.print();
    String str1("nihao");
    str1.print();
    str = str1;
    str.print();
    String str4 = str;
    str4.print();
    cout<<str4.c_str()<<endl;
    cout<<str4.length()<<endl;
    printf("str4 : %s\n",str4.c_str());
    return 0;
}